/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BackingBeans;

import ETL.ETL;
import Facebook.FacebookClientLocal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import smartgrid.Contract;
import smartgrid.Contracts;
import smartgrid.FaceBookAccount;
import smartgrid.FacebookAccountInterface;
import smartgrid.exceptions.CantRemoveFromDBexception;
import smartgrid.exceptions.FacebookAccountDisabledException;
import smartgrid.exceptions.FacebookAccountNotExistException;

/**
 *
 * @author claudio
 */
@ManagedBean
@SessionScoped
public class BackofficeBackingBean {

    private Map<String, Contract> Contracts;
    private String SelectedContract;
    private Map<String, FaceBookAccount> FacebooksMap;
    private String Selectedfacebook;
    private String Selectedfacebook2;

    /** Creates a new instance of BackofficeBackingBean */
    public BackofficeBackingBean() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        FacebookClientLocal fbl = null;
        InitialContext initCtx;
        FacebookAccountInterface fm;
        try {
            initCtx = new InitialContext();
            fm = (FacebookAccountInterface) initCtx.lookup("java:global/SmartGridEA/FacebookAccountManager");
            List<FaceBookAccount> fbs = fm.getFacebookAccounts();

            this.FacebooksMap = new TreeMap<String, FaceBookAccount>();
            for (FaceBookAccount f : fbs) {
                this.FacebooksMap.put(f.getNome() + "-" + f.getUId(), f);
            }
            this.Selectedfacebook = this.Selectedfacebook2 = FacebooksMap.keySet().iterator().next();

            Contracts si = (Contracts) initCtx.lookup("java:global/SmartGridEA/ContractsManager");
            List<Contract> ss = si.getContracts(this.FacebooksMap.get(this.Selectedfacebook2));
            this.Contracts = new HashMap<String, Contract>();
            if (ss != null) {
                System.out.println("NOT NULL");
                for (Contract s : ss) {
                    this.Contracts.put(s.getNumber(), s);
                }
                if (!ss.isEmpty()) {
                    this.SelectedContract = ss.get(0).getNumber();
                }
            }
        } catch (NamingException ex) {
            Logger.getLogger(BackofficeBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Set<String> getContractList() {
        try {
            InitialContext initCtx = new InitialContext();
            Contracts si = (Contracts) initCtx.lookup("java:global/SmartGridEA/ContractsManager");
            List<Contract> ss = si.getContracts(this.FacebooksMap.get(this.Selectedfacebook2));
            if (ss != null) {
                System.out.println("NOT NULL");
                for (Contract s : ss) {
                    this.Contracts.put(s.getNumber(), s);
                }
                if (!ss.isEmpty()) {
                    this.SelectedContract = ss.get(0).getNumber();
                }
            }
        } catch (NamingException ex) {
            Logger.getLogger(BackofficeBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return this.Contracts.keySet();
    }

    public void setSelectedfacebook(String Selectedfacebook) {
        this.Selectedfacebook = Selectedfacebook;
    }

    public void removeContract() {
        try {
            InitialContext initCtx = new InitialContext();
            Contracts fm = (Contracts) initCtx.lookup("java:global/SmartGridEA/ContractsManager");


            System.out.println(fm.removeContractFromFacebookAccount(this.FacebooksMap.get(this.Selectedfacebook2).getUId(), this.Contracts.get(this.SelectedContract).getNumber()));

        } catch (NamingException ex) {
            Logger.getLogger(BackofficeBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void etl(){
        ETL e = new ETL();
        System.out.println("-------------------------------------------------------------------");
        e.povoar();
    }

    public void setFacebookState() {
        try {
            InitialContext initCtx = new InitialContext();
            FacebookAccountInterface fm = (FacebookAccountInterface) initCtx.lookup("java:global/SmartGridEA/FacebookAccountManager");
            if (FacebooksMap.get(this.Selectedfacebook).getEnable()) {
                fm.disableFacebookAccount(FacebooksMap.get(this.Selectedfacebook).getUId());
            } else {
                fm.enableFacebookAccount(FacebooksMap.get(this.Selectedfacebook).getUId());
            }
        } catch (FacebookAccountDisabledException ex) {
            Logger.getLogger(BackofficeBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FacebookAccountNotExistException ex) {
            Logger.getLogger(BackofficeBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CantRemoveFromDBexception ex) {
            Logger.getLogger(BackofficeBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(BackofficeBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getFacebookStateInString() {
        try {
            InitialContext initCtx = new InitialContext();
            FacebookAccountInterface fm = (FacebookAccountInterface) initCtx.lookup("java:global/SmartGridEA/FacebookAccountManager");
            if (FacebooksMap.get(Selectedfacebook).getEnable()) {
                return "Desactivar Conta";
            } else {
                return "Activar conta";
            }
        } catch (NamingException ex) {
            Logger.getLogger(BackofficeBackingBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }

    public void setSelectedContract(String SelectedContract) {
        System.out.println("SELECTED " + SelectedContract);
        this.SelectedContract = SelectedContract;
    }

    public String getSelectedContract() {
        return SelectedContract;
    }

    public String getSelectedfacebook2() {
        return Selectedfacebook2;
    }

    public void setSelectedfacebook2(String Selectedfacebook2) {
        this.Selectedfacebook2 = Selectedfacebook2;
    }

    public Set<String> getFacebooksList() {
        return FacebooksMap.keySet();
    }

    public String getSelectedfacebook() {
        return Selectedfacebook;
    }

    public String getFacebookUID() {
        return FacebooksMap.get(Selectedfacebook).getUId();
    }

    public String getFacebookName() {
        return FacebooksMap.get(Selectedfacebook).getNome();
    }

    public Boolean getFacebookState() {
        return FacebooksMap.get(Selectedfacebook).getEnable();
    }
}
